<script lang="ts">
export default {
  name: 'Pay'
}
</script>

<script setup lang="ts">
import { message } from '@/components/XtxUI'
import { baseURL } from '@/utils/request'
import { useCountDown } from '@/hooks'
import { useCheckoutStore } from '@/store'
import { storeToRefs } from 'pinia'
import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const { orderId } = route.params
const router = useRouter()
const { start, countTimeText, countTime } = useCountDown()
const checkoutStore = useCheckoutStore()
const { orderDetail } = storeToRefs(checkoutStore)
checkoutStore.getOrderDetail(orderId as string).then(() => {
  // 初始化倒计时
  start(orderDetail.value?.countdown)
})
watch(countTime, (newVal) => {
  if (newVal <= 0) {
    router.replace('/cart')
    return message({ type: 'warn', text: '订单已超时，请重新下单' })
  }
})

// 支付宝网站申请沙箱测试账号(https://openhome.alipay.com/platform/appDaily.htm)
// 后台服务基准地址+支付接口地址+订单ID+回跳地址
// 支付宝的回跳地址不识别特殊符号，需要主动转码处理encodeURIComponent
// const baseURL = 'http://pcapi-xiaotuxian-front-devtest.itheima.net/'
const redirect = encodeURIComponent('http://www.corho.com:8080/#/member/pay/callback')
// 支付宝支付跳转链接
const alipayUrl = `${baseURL}pay/aliPay?orderId=${orderId}&redirect=${redirect}`
</script>

<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付订单</XtxBreadItem>
      </XtxBread>
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>
            支付还剩<span>{{ countTimeText }}</span
            >, 超时后将取消订单
          </p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ orderDetail?.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">
          选择以下支付方式付款（只支持支付宝支付，申请沙箱测试账号：https://openhome.alipay.com/platform/appDaily.htm）
        </p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="alipayUrl"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.pay-info {
  display: flex;
  align-items: center;
  padding: 0 80px;
  height: 240px;
  background: #fff;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    flex: 1;
    padding-left: 10px;

    p {
      &:first-child {
        margin-bottom: 5px;
        font-size: 20px;
      }

      &:last-child {
        font-size: 16px;
        color: #999;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        font-size: 20px;
        color: @priceColor;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  padding-bottom: 70px;
  background-color: #fff;

  p {
    padding-left: 30px;
    height: 70px;
    font-size: 16px;
    line-height: 70px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    display: inline-block;
    margin-left: 30px;
    width: 150px;
    height: 50px;
    color: #666666;
    text-align: center;
    line-height: 48px;
    border: 1px solid #e4e4e4;

    &.active,
    &:hover {
      border-color: @xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
</style>
